package cn.hfatec.shms.web.xc.xcReport.action;

import cn.hfatec.shms.cache.CodeInfoCache;
import cn.hfatec.shms.enums.CodeType;
import cn.hfatec.shms.web.yk.ykHsb.model.YkHsb;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.*;
import javax.annotation.Resource;


import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.web.xc.xcReport.model.XcReportParams;
import cn.hfatec.shms.web.xc.xcReport.model.XcReport;
import cn.hfatec.shms.web.xc.xcReport.service.XcReportService;
import cn.hfatec.shms.base.ShmsBaseAction;
import org.springframework.ui.Model;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;



/**
 *
 * @author hfatec codegen
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Controller
@RequestMapping("/xc/xcReport")
public class XcReportAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "XcReportService")
    private XcReportService xcReportService;


    /**
     *
     * 查询列表
     *
     */
    @RequestMapping("/list")
    public String list(XcReportParams xcReportParams, Page page, Model model) {

       Table table = xcReportService.queryXcReportTable(xcReportParams, page);

        List<XcReport>   xcList=xcReportService.queryXcReportXcList(xcReportParams);
        List<XcReport>   xhList=xcReportService.queryXcReportXhList(xcReportParams);
        List<XcReport>   weeklist=new ArrayList();
        DecimalFormat    df   = new DecimalFormat("######0.00");
        for (XcReport reportXc : xcList) {
            int yes_not=0;
            for (XcReport reportXh : xhList) {
            if(reportXc.getUserName().equals(reportXh.getUserName()))
               {  reportXc.setVisitComplete(reportXh.getVisitComplete());
                  reportXc.setYearRemind(reportXh.getYearRemind());
                   if(reportXc.getVisitPlan()!=null && reportXh.getVisitComplete()!=null && reportXc.getVisitPlan().toString().length()>0 && reportXh.getVisitComplete().toString().length()>0 && !reportXc.getVisitPlan().equals(0))
                   { reportXc.setVisitRate(Double.valueOf(df.format(reportXh.getVisitComplete() * 100 * 1.00 / reportXc.getVisitPlan()))); }
                   weeklist.add(reportXc);
                   yes_not=1;
               }

            }
            if(yes_not==0)
            { weeklist.add(reportXc);}
        }

        model.addAttribute("xcReportsTable", weeklist);
        model.addAttribute("xcReportParams", xcReportParams);
        model.addAttribute("userNameList", CodeInfoCache.getCodeInfo(CodeType.USERNAME_TYPE));
        return "/xc/xcReport/list";
    }

    /**
     *
     * 异步查询列表
     *
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(XcReportParams xcReportParams, Page page, Model model) {
        try {
            list(xcReportParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xc/xcReport/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 跳转到新增页
     *
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model) {
        try {
            XcReport xcReport = new XcReport();
            Calendar c = Calendar.getInstance();
            int week = c.get(Calendar.WEEK_OF_YEAR);
            xcReport.setWeekNum(String.valueOf(week));
            model.addAttribute("weekNum", week);
            model.addAttribute("xcReport", xcReport);
            model.addAttribute("userNameList", CodeInfoCache.getCodeInfo(CodeType.USERNAME_TYPE));
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xc/xcReport/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 跳转到编辑页
     *
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(Integer id, Model model) {

        try {
            XcReport xcReport = xcReportService.getXcReport(id);
            model.addAttribute("xcReport", xcReport);
            model.addAttribute("userNameList", CodeInfoCache.getCodeInfo(CodeType.USERNAME_TYPE));
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xc/xcReport/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 根据IDs删除数据
     *
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteXcReport(String ids) {
        try {
            List<String> idList = new ArrayList<String>();
            if(ids.length() > 0) {
                idList = Arrays.asList(ids.split(","));
            }
            xcReportService.deleteXcReportBatch(idList);
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("数据" + ids + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     *
     * 保存数据
     *
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveXcReport(@Valid XcReport xcReport, BindingResult result) {

        try {
            if(result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            xcReportService.saveXcReport(xcReport);
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("数据XcReport保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }
}
